package app.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.longevityassistant.activity.R;

public class ShareWeiboDialog extends Dialog {

	private Context context;
	private String shareContent;

	private String title;

	private View.OnClickListener mOnLogOutClickListener;

	private View.OnClickListener mOnShareListener;

	private View.OnClickListener mOnCloseListener;

	private TextView tvTitle;

	private EditText edShareContent;
	
	/**
	 * 微薄可分享的最大字数
	 */
	public static final int WEIBO_MAX_LENGTH = 140;

	/**
	 * 点击 按钮后， 是否自动隐藏
	 */
	private boolean isAutoDismiss = true;

	public ShareWeiboDialog(Context context) {
		super(context);
	}

	public ShareWeiboDialog(Context context, String title, String shareContent) {
		super(context);
		this.context = context;

		this.shareContent = shareContent;
		this.title = title;
		initView();
	}

	private void initView() {
		this.getContext().setTheme(android.R.style.Theme_InputMethod);

		setContentView(R.layout.app_widget_weibo_share_dialog);

		tvTitle = (TextView) findViewById(R.id.tv_title);
		tvTitle.setText(title);

		edShareContent = (EditText) findViewById(R.id.et_weibo_share);
		edShareContent.setText(shareContent);

		Window window = getWindow();
		WindowManager.LayoutParams attributesParams = window.getAttributes();
		attributesParams.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
		attributesParams.dimAmount = 0.4f;

		window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

		// 注销
		Button logOutButton = (Button) findViewById(R.id.btn_logout);
		logOutButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mOnLogOutClickListener != null) {
					mOnLogOutClickListener.onClick(v);
				}
				if (isAutoDismiss) {
					dismiss();
				}
			}
		});

		// 共享
		Button shareButton = (Button) findViewById(R.id.btn_share);
		shareButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				int length = edShareContent.length();
				if(length>WEIBO_MAX_LENGTH){
					Toast.makeText(context, "分享内容最大长度为"+WEIBO_MAX_LENGTH+
							"个字,现在长度为"+length+"个字，请再做修改。", Toast.LENGTH_LONG).show();
					return;
				}
				
				if (mOnShareListener != null) {
					mOnShareListener.onClick(v);
				}
				if (isAutoDismiss) {
					dismiss();
				}
			}
		});

		// 关闭
		Button closeButton = (Button) findViewById(R.id.btn_close);
		closeButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mOnCloseListener != null) {
					mOnCloseListener.onClick(v);
				}
				if (isAutoDismiss) {
					dismiss();
				}
			}
		});
	}

	/**
	 * 注销监听器
	 */
	public void setOnLogOutListener(View.OnClickListener onClickListener) {
		mOnLogOutClickListener = onClickListener;
	}

	/**
	 * 分享监听器
	 */
	public void setOnShareListener(View.OnClickListener onClickListener) {
		mOnShareListener = onClickListener;
	}

	/**
	 * 关闭监听器
	 */
	public void setOnCloseListener(View.OnClickListener onClickListener) {
		mOnCloseListener = onClickListener;
	}

	/**
	 * 获取共享内容
	 */
	public String getShareContent() {
		return edShareContent.getText().toString();
	}

	/**
	 * 赋值共享内容
	 */
	public void setShareContent(String shareContent) {
		edShareContent.setText(shareContent);
	}

	/**
	 * 设置标题
	 */
	public void setTitle(String title) {
		tvTitle.setText(title);
	}
}
